package com.huawei.eservice.demo;

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.android.AndroidElement; 

import java.io.File;

import java.net.URL;

import java.util.List;


import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ContactsTest {

    private AppiumDriver driver;

    @BeforeClass
    public void setUp() throws Exception {

        // set up appium

        File classpathRoot = new File(System.getProperty("user.dir"));

        File appDir = new File(classpathRoot, "/apps");

        File app = new File(appDir, "ContactManager.apk");

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("deviceName","emulator-5556");

        capabilities.setCapability("platformVersion", "6.0");

        capabilities.setCapability("app", app.getAbsolutePath());

        capabilities.setCapability("appPackage", "com.example.android.contactmanager");

        capabilities.setCapability("appActivity", ".ContactManager");

        driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

        System.out.println("App is launched!");

    }

    @Test
    public void addContact(){

        WebElement el = driver.findElement(By.name("Add Contact"));

        el.click();

        List<AndroidElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");

        textFieldsList.get(0).sendKeys("Some Name");

        textFieldsList.get(2).sendKeys("Some@example.com");

//        driver.swipe(100, 500, 100, 100, 2);

        driver.findElementByName("Save").click();

        System.out.println("App is done!");

    }

    @AfterClass
    public void tearDown() throws Exception {

        driver.quit();

    }

}